package com.niiwoo.civet.trade.utils;

import com.niiwoo.civet.trade.constant.DatePatternConstant;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.enums.ProjectStatusEnum;
import com.niiwoo.civet.trade.enums.ProjectSubStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;

import java.util.Date;

/**
 * @author: liaozuliang
 * @date: 2018/5/12
 */
@Slf4j
public class ProjectUtil {

    /**
     * 获取标的发布时间
     * @param date
     * @return
     */
    public static String getProjectPublishtime(Date date) {
        try {
            return getDateTimeStr(date) + " 发布";
        } catch (Exception e) {
            log.error("获取标的发布时间出错：", e);
        }
        return "";
    }

    /**
     * 获取标的申请/发布/满标时间
     * @param project
     * @return
     */
    public static String getProjectPublishtime(Project project) {
        try {
            String publishTime = getDateTimeStr(project.getCreateTime()) + " 申请";

            ProjectStatusEnum statusEnum = ProjectStatusEnum.enumOf(project.getStatus());
            if (statusEnum == ProjectStatusEnum.COLLECTING && ProjectSubStatusEnum.COLLECTING_INIT == ProjectSubStatusEnum.enumOf(project.getSubStatus())) {
                publishTime = getDateTimeStr(project.getPublishTime()) + " 发布";
            } else if (statusEnum == ProjectStatusEnum.REPAYING || statusEnum == ProjectStatusEnum.FINISH) {
                publishTime = getDateTimeStr(project.getFullSuccessTime()) + " 满标";
            } else if (statusEnum == ProjectStatusEnum.COLLECTING
                    && ProjectSubStatusEnum.COLLECTING_LENDING == ProjectSubStatusEnum.enumOf(project.getSubStatus())) {
                publishTime = getDateTimeStr(project.getFullSuccessTime()) + " 满标";
            }

            return publishTime;
        } catch (Exception e) {
            log.error("获取标的申请/发布/满标时间出错：", e);
        }

        return "";
    }

    public static String getDateTimeStr(Date date) {
        String dateTimeStr = "";
        if (com.niiwoo.tripod.base.utils.DateUtils.isToday(date)) {
            dateTimeStr = FastDateFormat.getInstance(DatePatternConstant.HH_MM).format(date);
        } else {
            dateTimeStr = FastDateFormat.getInstance(DatePatternConstant.YYYY_MM_DD_HH_MM).format(date);
        }
        return dateTimeStr;
    }
}
